<?php namespace App\Model;

use App\Lib\Params\InfoSearchParams;
//use App\Lib\Params\BaseWebParams;

class Info extends BaseModel {

    protected $fillable = [
        '*'
    ];
	//
    public function webColumn()
    {
        return $this->belongsTo('App\Model\WebColumn','column_id');
    }

    public function contents(){
        return $this->hasMany('App\Model\Content','info_id','id');
    }

    public static function search(InfoSearchParams $infoSearchParams,$select=array(),$pageSize=25)
    {
        $selectFiled = self::getSelectField($select);
        $infos = self::select($selectFiled);
        $infos->where('is_del' , '=', 0);

        if(!empty($infoSearchParams->businessId)){
            $infos->where('business_id' , '=', $infoSearchParams->businessId);
        }
        if(!empty($infoSearchParams->secondColumnId)){
            $infos->where('column_id', '=', $infoSearchParams->secondColumnId);
        }
        if(!empty($infoSearchParams->title)){
            $infos->where('title', 'like', '%'.$infoSearchParams->title.'%');
        }
        return $infos->orderBy('order_id','asc')->orderBy('create_time','desc')->paginate($pageSize);
    }

    public static function getInfoById($infoId,$select=array())
    {
        if(empty($infoId) || !is_numeric($infoId) || $infoId < 0){
            return null;
        }
        $selectFiled = self::getSelectField($select);
        return self::select($selectFiled)->find($infoId);
    }

    public static function addInfo($info)
    {
        if(empty($info)){
            return false;
        }
        return self::insertGetId($info);
    }
    public static function updateInfoById($infoId,$infoData)
    {
        if(!is_numeric($infoId) || $infoId <= 0 || empty($infoData)){
            return false;
        }
        $info = self::find($infoId);
        if(is_null($info)){
            return false;
        }
        foreach($infoData as $key=>$value){
            $info->$key = $value;
        }
        return $info->save();
//        return self::find($infoId)->update($infoData);
    }
    public static function deleteInfo($infoId)
    {
        if(!is_numeric($infoId) || $infoId <= 0){
            return false;
        }
        $info =  self::find($infoId);
        if(is_null($info)){
            return false;
        }
        $info->is_del = 1;
        return $info->save();
    }
    private static function updateData($archives,$data){
        foreach($data as $key=>$value){
            $archives->$key = $value;
        }
        return $archives->save();
    }
}
